﻿using System;
using System.Collections.Generic;
using System.Linq;
using AlexJamesBrown.JoeBlogs;
using AlexJamesBrown.JoeBlogs.Structs;

namespace Social.Wordpress
{
    public class Wordpress : ISocialNetwork
    {
        WordPressWrapper wrapper = null;
        IWordpressConfig config;

        #region Constructors

        public Wordpress(IWordpressConfig wpConfig)
        {
            config = wpConfig;
            wrapper = new WordPressWrapper(config.BlogUrl + "/xmlrpc.php", config.Username, config.Password);
        }

        #endregion Constructors

        #region Methods

        public ContentServiceResponse<string> Publish(string title, string description)
        {
            ContentServiceResponse<string> response = new ContentServiceResponse<string>();

            var post = new Post();
            post.dateCreated = DateTime.UtcNow; ; //since this is a struct, we can't have a constructor that does this!
            post.title = title;
            post.description = description;

            try
            {
                response.Content = wrapper.NewPost(post, true);
                response.ResultOption = ServiceResultOptions.Success;
            }
            catch (Exception ex)
            {
                response.SetException(ex);
            }
            return response;
        }

        public ContentServiceResponse<Post> GetPost(string postID)
        {
            var response = new ContentServiceResponse<Post>();
            try
            {
                response.Content = wrapper.GetPost(postID);
                response.ResultOption = ServiceResultOptions.Success;
            }
            catch (Exception ex)
            {
                response.ResultOption = ServiceResultOptions.SystemException;
                response.Message = ex.Message;
            }
            return response;
        }

        public int GetAllCommentsCount(string id)
        {
            //return wrapper.GetCommentCount(id).total_comments;
            return GetComments(id).Count();
        }

        public IList<Category> GetCategories()
        {
            return wrapper.GetCategories();
        }

        public List<Comment> GetComments(string postID)
        {
            var result = new List<Comment>();
            var cur = new List<Comment>();
            var index = 0;
            var commentsAtATime = 1000;
            try
            {
                do
                {
                    cur = wrapper.GetComments(postID, "", commentsAtATime, index).ToList();
                    result.AddRange(cur);
                    index += commentsAtATime;
                }
                while (cur.Count == commentsAtATime);
            }
            catch (Exception)
            {
                result = null;
            }

            return result;
        }

        #endregion Methods

        #region ISocialNetwork Members

        public void Authenticate()
        {
            config.SaveCredentails();
        }

        public string ConnectUrl
        {
            get { return "/Pages/Settings/WordPressConnect.aspx"; }
        }

        /// <summary>
        /// These properties and methods have no relevance in Wordpress
        /// </summary>
        public bool IsAuthenticated
        {
            get { return false; }
        }

        public void Login()
        {
        }

        #endregion ISocialNetwork Members
    }
}